/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.actionsystem.gdp.dao;

import br.com.actionsystem.gdp.modelo.Reservas;
import br.com.actionsystem.gdp.util.DaoException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author aluno
 */
public class ReservaDao implements IntReservasDao {

    EntityManager em;
    Query query;

    public void salvar(Reservas reserva) throws DaoException {
        try {
            em = ConexaoSingleton.getConexao();
            em.getTransaction().begin();
            em.persist(reserva);
            em.flush();// tenta grava senao ele da rolback
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new DaoException(e.getMessage());
        }
    }
    public List<Reservas> listaTodas() {
        em = ConexaoSingleton.getConexao();
        query = em.createQuery("from Reservas");
        return query.getResultList();
    }

    public Reservas buscaReserva(String nome){
        Reservas r = new Reservas();
        em = ConexaoSingleton.getConexao();
        query = em.createQuery("from Reservas where nomeBanco = :nomeReserva");
        query.setParameter("nomeReserva", nome);
        return r = (Reservas) query.getSingleResult();
    }

    public void delete(Reservas reserva)throws  DaoException{
        try {
        em = ConexaoSingleton.getConexao();
        em.getTransaction().begin();
        em.remove(reserva);
        em.getTransaction().commit();

        } catch (Exception e) {
            em.getTransaction().rollback();
            throw  new DaoException("não foi possivel deletar está reserva ",e);
        }
   

    }
}
